/* Copyright (c) [2019年5月1日] [吴超]
[MBT_Studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
 */
#include "stdafx.h"
#include "材质纹理插座.h"
#include "节点/节点树.h"

using namespace std;


C_图像插座::C_图像插座(wstring name) :C_插座基类(name, E_值类型::e_Type_图像) {
	m_像素.m_维度 = {0,0,0};
}

C_图像插座::~C_图像插座() {
}

void* C_图像插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_图像) {
			return (socket)->f_getData(线程ID);
		}
	}
	return &m_像素;
}

void C_图像插座::f_setData(void* data, int64 ID, uint8 线程ID) {
	
}

void C_图像插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
}

void C_图像插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
}









C_纹理插座::C_纹理插座(wstring name, S_设备环境& ctx) :C_插座基类(name, E_值类型::e_Type_纹理) {
	m_内部空纹理 = {};
	//m_Tex = f_tex_创建图像纹理(ctx, {64,64,1}, E_纹理格式::e_tf_RGBA8U_U, E_纹理维度类型::e_2D);
	//std::vector<uint8> data(64 * 64 * 4, 100);
	//f_tex_填充纹理数据(m_Tex, data.data(), sizeof(S_RGBA8UI), { 64,64,1 });
	m_Tex = &m_内部空纹理;
}

C_纹理插座::~C_纹理插座() {
}

void* C_纹理插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_纹理) {
			return (socket)->f_getData(线程ID);
		}
	}
	else {
		if (f_get方向() == E_插座方向::e_插座Type_输入) {
			m_Tex = &m_内部空纹理;
		}
	}
	return m_Tex;
}

void C_纹理插座::f_setData(void* data, uint8 线程ID) {
	m_Tex = (S_纹理*)data;
}

std::string C_纹理插座::f_构建变量代码(int32 loc) {
	C_插座基类* socket = f_getLinkOutSocket(loc);

	auto* rt = m_pNode->f_getThis节点树();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_纹理) {
			m_Code = socket->f_getCode(0);
			return "";
		}

		
		switch (rt->m_树类型) {
		case E_节点树类型::e_type_着色节点树: {
			//m_Code = f_整数值转字符串(m_内部空纹理.m_ID);
			m_Code = socket->f_getCode(0);
			break;
		}
		default:
			switch (socket->m_Type) {
			case E_值类型::e_Type_Vec3:
				m_Code = socket->f_getCode(0);
				break;
			default:
				break;
			}
			break;
		}

	}
	else {
		switch (rt->m_树类型) {
		case E_节点树类型::e_type_着色节点树: {
			//C_插座基类::f_构建变量代码(loc);
			//m_Code = "(&" + m_Code + ")";
			m_Code = f_整数值转字符串(m_内部空纹理.m_ID);
			break;
		}
		default:
			m_Code = f_代码构建_变量(f_getNode()->f_getThis节点树(), &(m_Tex->m_默认颜色), E_值类型::e_Type_Vec3);
			break;
		}
	}

	return "";
}



bool C_纹理插座::f_接入插座是否匹配(E_值类型 type) {
	switch (m_Type) {
	case E_值类型::e_Type_Vec3:
	case E_值类型::e_Type_纹理:
		return true;
	}
	return false;
}

void C_纹理插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	//fread(&m_Type, sizeof(uint32), 1, f);
	fread(&m_内部空纹理.m_默认颜色, sizeof(vec3), 1, f);
}

void C_纹理插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
	fwrite(&m_内部空纹理.m_默认颜色, sizeof(vec3), 1, f);
}





C_纹理1D插座::C_纹理1D插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_纹理1D) {
	m_默认 = (S_TexArray*)f_core_array_new(0, sizeof(S_纹理*));
	m_转换 = (S_TexArray*)f_core_array_new(0, sizeof(S_纹理*));
	m_Value = m_默认;

	//m_默认->ptr_userData[0] = &m_内部空纹理;
}

C_纹理1D插座::~C_纹理1D插座() {
	f_core_array_free((S_Array*)m_默认);
	f_core_array_free((S_Array*)m_转换);
}

void* C_纹理1D插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_纹理1D) {
			return DEF_纹理插座_1D数据(socket, 0);
		}
		else if (socket->m_Type == E_值类型::e_Type_纹理) {
			f_core_array_resize((S_Array*)m_转换, 1);
			m_转换->ptr_userData[0] = DEF_纹理插座数据(socket);
		}

		m_Value = m_转换;
	}
	else {
		m_Value = m_默认;
	}
	return m_Value;
}

void C_纹理1D插座::f_setData(void* data, uint8 线程ID) {
	this;
}

std::string C_纹理1D插座::f_构建变量代码(int32 loc) {
	C_插座基类* socket = f_getLinkOutSocket(loc);

	auto* rt = m_pNode->f_getThis节点树();
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_纹理1D) {
			m_Code = socket->f_getCode(0);
			return "";
		}



		switch (rt->m_树类型) {
			case E_节点树类型::e_type_着色节点树: {
				//m_Code = f_整数值转字符串(m_内部空纹理.m_ID);
				m_Code = socket->f_getCode(0);
				break;
			}
			default: {
				switch (socket->m_Type) {
					case E_值类型::e_Type_Vec3:
						m_Code = socket->f_getCode(0);
						break;

					case E_值类型::e_Type_纹理 :{
						
						break;
					}
				}
				break;
			}
		}


	}
	else {
		switch (rt->m_树类型) {
			case E_节点树类型::e_type_着色节点树: {
				m_Code = f_整数值转字符串(m_内部空纹理.m_ID);
				break;
			}
			default:
				C_插座基类::f_构建变量代码(loc);
				//m_Code = f_代码构建_变量(f_getNode()->f_getThis节点树(), &(m_Tex->m_默认颜色), E_值类型::e_Type_Vec3);
				break;
		}
	}

	return "";
}

void C_纹理1D插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
	S_文件块 文件块 = f_file_读取文件块(f);
	if (文件块.m_块类型 == E_节点工程文件块类型::e_工程文件块_节点实例属性) {
		const uint8* 下一次读取数据指针 = 文件块.m_data;

	}
	free(文件块.m_data);
}

void C_纹理1D插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

	auto 文件块 = f_file_构建文件块信息(E_节点工程文件块类型::e_工程文件块_节点实例属性, S_节点数据::g_文件版本号);
	S_UI8Array* 块 = (S_UI8Array*)f_core_array_new(0, sizeof(uint8));

	f_file_保存文件块(f, 文件块, 块);
}




C_过程纹理属性插座::C_过程纹理属性插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_TexProp) {
	m_默认 = f_alloc_纹理属性(nullptr, L"纹理属性");
	m_TexProp = &m_默认;
}

C_过程纹理属性插座::~C_过程纹理属性插座() {
}

void* C_过程纹理属性插座::f_getData(uint8 线程ID) {
	C_插座基类* socket = f_getLinkOutSocket(线程ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_TexProp) {
			return socket->f_getData(0);
		}
	}
	return m_TexProp;
}

void C_过程纹理属性插座::f_setData(void* data, uint8 线程ID) {
	this;
}

void C_过程纹理属性插座::f_读取(FILE* f) {

}

void C_过程纹理属性插座::f_写入(FILE* f) {

}



C_材质插座::C_材质插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_材质) {
	m_Value = nullptr;
}

C_材质插座::~C_材质插座() {

}

void* C_材质插座::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_材质) {
			return socket->f_getData(当前插座连接线ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_材质1D) {
			auto* mat = DEF_材质插座_1D数据(socket, 当前插座连接线ID);
			if (mat->count) {
				return mat->ptr_userData[0];
			}
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			//return DEF_物体插座数据(socket, 当前插座连接线ID);

		}
		else if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {

		}
	}
	return m_Value;
}

void C_材质插座::f_setData(void* data, uint8 线程ID) {
	C_插座基类::f_setData(data);

	m_Value = (S_材质*)data;
}

bool C_材质插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_材质:
	case E_值类型::e_Type_材质1D:
		return true;
	}
	return false;
}

void C_材质插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);

}

void C_材质插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);

}



C_材质_1D插座::C_材质_1D插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_材质1D) {
	m_默认值 = (S_MaterialArray*)f_core_array_new(0, sizeof(S_材质*));
	m_Value = m_默认值;
}

C_材质_1D插座::~C_材质_1D插座() {
	//f_core_array_free((S_Array*)m_Value);
	f_core_array_free((S_Array*)m_默认值);
}

void* C_材质_1D插座::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_材质1D) {
			return DEF_材质插座_1D数据(socket, 当前插座连接线ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_材质) {
			f_core_array_resize((S_Array*)m_默认值, 1);
			*((S_材质**)f_core_array_at((S_Array*)m_默认值, 0)) = DEF_材质插座数据(socket, 当前插座连接线ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_Object) {
			//return DEF_物体插座数据(socket, 当前插座连接线ID);
		}
		else if (socket->m_Type == E_值类型::e_Type_Object_Array1D) {

		}

		m_Value = m_默认值;
	}
	else if (!m_映射指针) {
		m_Value = m_默认值;
	}
	return m_Value;
}

void C_材质_1D插座::f_setData(void* data, uint8 线程ID) {
	C_插座基类::f_setData(data);

	if (data) {
		m_Value = (S_MaterialArray*)data;
		m_映射指针 = true;
	}
	else {
		m_Value = m_默认值;
		m_映射指针 = false;
	}
}

bool C_材质_1D插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_材质1D:
		return true;
	}
	return false;
}

void C_材质_1D插座::f_读取(FILE* f) {
	C_插座基类::f_读取(f);
}

void C_材质_1D插座::f_写入(FILE* f) {
	C_插座基类::f_写入(f);
}




C_着色器插座::C_着色器插座(std::wstring name) :C_插座基类(name, E_值类型::e_Type_Shader) {
	m_默认值 = new S_节点实例着色器();
	m_Value = m_默认值;
}

C_着色器插座::~C_着色器插座() {
}

void* C_着色器插座::f_getData(uint8 当前插座连接线ID) {
	C_插座基类* socket = f_getLinkOutSocket(当前插座连接线ID);
	if (socket) {
		if (socket->m_Type == E_值类型::e_Type_Shader) {
			return socket->f_getData(0);
		}
		else if (socket->m_Type == E_值类型::e_Type_材质) {
			f_core_array_resize((S_Array*)m_默认值, 1);
			*((S_材质**)f_core_array_at((S_Array*)m_默认值, 0)) = DEF_材质插座数据(socket, 当前插座连接线ID);
		}

		m_Value = m_默认值;
	}
	return m_Value;
}

void C_着色器插座::f_setData(void* data, uint8 线程ID) {
	m_Value = (S_着色*)data;
}

bool C_着色器插座::f_接入插座是否匹配(E_值类型 type) {
	switch (type) {
	case E_值类型::e_Type_Shader:
		return true;
	}
	return false;
}
